package com.maigao.entity.jpa;

import lombok.Data;
import lombok.ToString;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Data
@Entity
@Table(name = "tb_student")
@DynamicInsert
@DynamicUpdate
public class Student implements Serializable {
    public enum Status{
        NORMAL(0), DISABLED(-1);
        Status(int value) {
            this.value = value;
        }
        private int value;

        public int getValue() {
            return value;
        }

        public static Status getByValue(int value) {
            Optional<Status> optional = Arrays.stream(values()).filter(p -> Objects.equals(p.getValue(), value)).findFirst();
            if (optional.isPresent()) {
                return optional.get();
            }
            return null;
        }
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "user_uid")
    private String userUid;

    @Column(name = "student_name")
    private String studentName;

    @Column(name = "coach_id")
    private Integer coachId;

    @Column(name = "coach_name")
    private String coachName;

    @Column(name = "status")
    private Integer status;

    @Column(name = "create_time")
    private LocalDateTime createTime;

    /*@OneToMany(mappedBy = "student")
    private List<TakeOffRecord> takeOffRecords;*/
    @ToString.Exclude   //解决查询时无限递归问题
    @OneToMany(mappedBy = "student", cascade = CascadeType.ALL)
    private List<QueryStudentCourse> studentCourses;
}
